
import { address, wxLogin } from '../../../api/eapi.js'


Page({
  data:{
    dataList: []
  },
  goBack(){
    wx.navigateBack()
  },
  onLoad() {
    wx.showLoading({title: `加载中`});
  	// wxLogin().then(resp => {
			address.get().then(resp => {
				console.log(resp);
				this.setData({dataList: resp.data}, () => {
          wx.hideLoading();
          wx.setStorage({key:`address_change`, data: `false`})
        });
			}).catch(err => {
        wx.hideLoading();
		    console.error(err);
			})
  	// }).catch(err => {
  	// 	console.error(err);
  	// });
  },
  onShow() {
		wx.getStorage({
			key: `address_change`,
			success: (res) => {
				if(res.data === `true`) {
						address.get().then(resp => {
							this.setData({dataList: resp.data}, () => wx.setStorage({key:`address_change`, data: `false`}));
						}).catch(err => {
							if(err.code === `401`) address.get().then(resp => this.setData({dataList: resp.data}, () => wx.setStorage({key:`address_change`, data: `false`})));
							console.error(err);
						})
				}
			}
		})
  },
  tapHandle(e) {
  	let type = e.target.dataset.type;
  	let id = e.target.dataset.id;
  	let isDefault = e.target.dataset.default === 1 ? true : false;
  	switch(type) {
  		case 'setDefault':
  		if(!isDefault) {
  			address.setDefault(id).then(resp => {
  				address.get().then(resp => {
  					this.setData({dataList: resp.data}, () => wx.showToast({title: `设置默认成功`, icon: `none`}));
            wx.setStorage({
              key: `me_address_change`,
              data: `true`
            })
  				});
  			})
  		}
  	}
  }
})
